<?php

namespace App\Tasks;

use App\Clients\Keruyun\IncomeListByPage;
use App\Clients\Keruyun\StoreQueryBill;
use App\Clients\Keruyun\StoreQueryBillCompleted;
use App\Models\MallDateFinance;
use App\Models\ProjectMall;
use GuzzleHttp\Exception\GuzzleException;

class KeruyunDatasSyncTask extends MeiTuanMallTask
{
    /**
     * @throws GuzzleException
     */
    public function execute(): void
    {
        $startDate = $this->maxDate();
        //$startDate = "2025-09-19";
        $diffDate = (strtotime(date("Y-m-d",time())) - strtotime(date("Y-m-d",strtotime($startDate)))) / 86400;
        foreach (range(0,$diffDate,1) as $index=>$item){
            $beginDate = date("Y-m-d H:i:s",strtotime($startDate) + 1 * $index * 86400);
            $endDate = date("Y-m-d H:i:s",strtotime($beginDate) + 86399);
            $queryDates[] = [
                "merchantNo"=>$this->mall->merchant_no,
                "startDate"=>intval(strtotime($beginDate) * 1000),
                "endDate"=>intval(strtotime($endDate) * 1000),
            ];
        }
        $incomClient = new IncomeListByPage($this->account);
        $billClient = new StoreQueryBill($this->account);
        $billDetail = new StoreQueryBillCompleted($this->account);
        foreach ($queryDates as $queryDate){
            $udate = date("Y-m-d",intval($queryDate["startDate"] / 1000));
            var_dump($udate);
            $model = MallDateFinance::query()
                ->where("merchant_no",$this->mall->merchant_no)
                ->where("date",$udate)
                ->first();
            if(!empty($model) && $model->is_update == 1){
                continue;
            }
            var_dump("营业额->".date("Y-m-d",$queryDate["startDate"] / 1000));
            $incomClient->handleQueryData($queryDate);
            $response = $incomClient->loopExecute(null)->contents();
            $income = empty($response["data"]["values"]) ? 0 : $response["data"]["values"][0]["businessIncomeAmt"];
            $billStartDate = intval((($queryDate["startDate"] / 1000) + 86400) * 1000);
            var_dump("分账日->".date("Y-m-d",$billStartDate / 1000));
            $query = [
                "poId"=>$this->mall->poi_id,
                "billStartDate"=>$billStartDate,
                "billEndDate"=>$billStartDate,
            ];
            $subDatas = json_decode($this->mall->sub_merchant_date,true);
            foreach ($subDatas as $subData){
                if(
                    intval($billStartDate / 1000) >= strtotime($subData["s"]) &&
                    intval($billStartDate / 1000) <= strtotime($subData["e"]) &&
                    !empty($subData["pid"])
                ){
                    var_dump($subData["pid"]);
                    $query["projectIdList"] = [
                        $subData["pid"]
                    ];
                }
            }
            //exit();

            $billClient->handleQueryData($query);
            $response = $billClient->loopExecute(null)->contents();
            $subaoumt = 0;
            $billNo = "";
            if(!empty($response["data"]["list"])){
                $billNo = $response["data"]["list"][0]["billNo"];
                $amount = $response["data"]["list"][0]["amount"];
                $billDetail->handleQueryData(["billNo"=>$billNo]);
                $response = $billDetail->loopExecute(null)->contents();
                $subMerchantId = $this->mall->sub_merchant_id;
                $list = $response["data"]["shopBillDTO"]["payeeBillDTOList"];
                foreach ($list as $lit){
//                    var_dump($lit["payeeId"]);
//                    var_dump($subMerchantId);
                    if(in_array($lit["payeeId"], $subMerchantId)){
                        $subaoumt = $lit["amount"];
//                        var_dump($subaoumt);
//                        exit();
                        break;
                    }
                }

            }
            //exit();
            empty($model) and $model = new MallDateFinance;
            $model->bill_no = $billNo;
            $model->merchant_no = $this->mall->merchant_no;
            $model->date = $udate;
            $model->income = $income;
            $model->settle_money = $subaoumt;
            $model->save();
            var_dump("------------------------------------");
        }
        $this->updateIncom();

    }
}
